
package es.ceu.mp2.conway;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;

public class ConwayGUI extends JFrame{

    private JComponent[][] buttons;
    private JSlider speed = new JSlider();
    private JLabel speedLabel = new JLabel(" Speed");
    private JButton play = new JButton("Play");
    private JButton pause = new JButton("Pause");
    private JButton nextStep = new JButton("Next");
    private JLabel iterations = new JLabel("   Iteration: 0");
    private boolean replayed = false;
    private boolean paused = false;
    private boolean nextStepClicked = false;

    public ConwayGUI(GameBoard board, int maxIter, String gameName) {

        JPanel gameBoard = new JPanel();
        JPanel components = new JPanel();
        JPanel componentsAndIterations = new JPanel();
        JPanel iterationsPanel = new JPanel();
        JLabel maxIterations = new JLabel("Max. Iterations: " + maxIter);

        this.setIconImage(Toolkit.getDefaultToolkit().getImage("./images/logo.png"));


        int rows = board.getRows();
        int columns = board.getColumns();
        int size = 25;
        if(rows > 28 || columns > 50)
            size = 20;
        if(rows > 35 || columns > 60)
            size = 15;
        //if(rows > 48 || columns > 85)   valor maximo

        setTitle(gameName);

        gameBoard.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        gameBoard.setLayout(new GridLayout(rows, columns));
        components.setLayout(new GridLayout(1,1));
        iterationsPanel.setLayout(new GridLayout(1,1));
        componentsAndIterations.setLayout(new GridLayout(2,1));

        buttons = new JComponent[rows][columns];

        for (int i = 0; i < rows; i++)
            for (int j = 0; j < columns; j++){
                buttons[i][j] = new JPanel();
                buttons[i][j].setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
                buttons[i][j].setBackground(Color.BLACK);
                gameBoard.add(buttons[i][j]);
            }

        gameBoard.setPreferredSize(new Dimension(size*columns, (size*rows) + 26));
        add(gameBoard);
        
        components.add(speed);
        components.add(speedLabel);
        components.add(play);
        components.add(pause);
        components.add(nextStep);
        
        iterationsPanel.add(iterations);
        iterationsPanel.add(maxIterations);

        componentsAndIterations.add(components);
        componentsAndIterations.add(iterationsPanel, BorderLayout.SOUTH);

        add(componentsAndIterations, BorderLayout.SOUTH);

        setSize(size*columns - 22, (size*rows) + 26);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public boolean printBoard(GameBoard board){
        pause.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                paused = true;     
          }
        });

        int rows = board.getRows();
        int columns = board.getColumns();
        for (int i = 0; i < rows; i++)
            for (int j = 0; j < columns; j++)
                if(board.isAlive(i, j))
                    buttons[i][j].setBackground(Color.YELLOW);
                else
                    buttons[i][j].setBackground(Color.BLACK);
        if(paused){
            paused = false;
            return true;
        }
        return false;
    }

    public boolean pause(){
        boolean playPressedNextStepNot = false;
        Thread appThread = new Thread() {
            @Override
            public void run() {
                play.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        replayed = true;
                    }
                });

                nextStep.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        nextStepClicked = true;
                    }
                });
            }
        };
        appThread.start();
        while(!replayed && !nextStepClicked){}
        if(replayed)
            playPressedNextStepNot = true;
        if(nextStepClicked)
            playPressedNextStepNot = false;
        replayed = false;
        nextStepClicked = false;

        return playPressedNextStepNot;
    }

    public int getSpeed(){
        return speed.getValue();
    }

    public void setIterations(int i){
        iterations.setText("   Iteration: " + String.valueOf(i));
    }


}
